package com.lsy.weizhi2.mybase;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.myutils.TitleViewCall;

/**
 * Created by 嗨哟 on 2017/3/13.
 */
public class MyTitleView extends RelativeLayout implements View.OnClickListener{
    private Context context;
    private Boolean ok_b,more_b;
    private String title,ok;
    private TextView titleName,titleOk;
    private ImageView titleBack,titleMore;
    private TitleViewCall titleViewCall;
    private RelativeLayout back_rl;
    private boolean back;
    private int titlemore;
    public MyTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        LayoutInflater.from(context).inflate(R.layout.my_title_view5,this,true);
        TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyTitleView);
        title=typedArray.getString(R.styleable.MyTitleView_titleName);
        ok=typedArray.getString(R.styleable.MyTitleView_titleOk);
        ok_b=typedArray.getBoolean(R.styleable.MyTitleView_titleOk_b,true);
        more_b=typedArray.getBoolean(R.styleable.MyTitleView_titleMore_b,false);
        titlemore=typedArray.getResourceId(R.styleable.MyTitleView_titleMore,0);
        back=typedArray.getBoolean(R.styleable.MyTitleView_titleBack,true);
        typedArray.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        back_rl= (RelativeLayout) findViewById(R.id.back_rl);
        titleName= (TextView) findViewById(R.id.my_title_tv);
        titleOk= (TextView) findViewById(R.id.my_title_ok);
        titleMore= (ImageView) findViewById(R.id.my_title_iv);
        titleBack= (ImageView) findViewById(R.id.my_title_back);
        if(!TextUtils.isEmpty(title)){
            titleName.setText(title);
        }
        if(!TextUtils.isEmpty(ok)){
            titleOk.setText(ok);
        }
        if(ok_b){
            titleOk.setVisibility(View.VISIBLE);
        }else{
            titleOk.setVisibility(View.GONE);
        }
        if(more_b){
            titleMore.setVisibility(View.VISIBLE);
            titleOk.setVisibility(View.GONE);
            if(titlemore!=0){
                titleMore.setImageResource(titlemore);
            }
        }
        if(!back){
            back_rl.setBackgroundColor(getResources().getColor(R.color.touming));
            titleName.setVisibility(View.GONE);
        }
        titleOk.setOnClickListener(this);
        titleMore.setOnClickListener(this);
        titleBack.setOnClickListener(this);
    }

    public void setTitleViewCall(TitleViewCall titleViewCall){
        this.titleViewCall=titleViewCall;
    }

    public void setOk(String ok){
        titleOk.setText(ok);
    }

    public String getOk(){
        return titleOk.getText().toString().trim();
    }

    public void setTitle(String title){
        titleName.setText(title);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.my_title_ok:
                titleViewCall.Click(view,TitleViewCall.OK);
                break;
            case R.id.my_title_iv:
                titleViewCall.Click(view,TitleViewCall.MORE);
                break;
            case R.id.my_title_back:
                ((Activity)context).finish();
                break;
        }
    }
}
